#include<stdio.h>
#include<stdlib.h>
typedef struct GNode *PtrToGNode;
#define inf 66666
struct GNode{
    int v;  //顶点数
    int e;  //边数
    int g[100][100];
};
typedef PtrToGNode Graph;
bool vis[100];   //访问标记
int n, m;

void init(){
    for (int i = 0; i <n;i++){
            vis[i] = false;
    }
}

Graph CreateGraph(){

    Graph G;

    G=(Graph)malloc(sizeof(struct GNode));
    G->v=n;
    G->e = 0;

    for (int i = 0;i<n;i++){
        for (int j = 0;j<n;j++){
            G->g[i][j] = inf;
            
        }
    }
    init();
    return G;
}

void InsertEdge(Graph G,int a,int b,int w){

    G->g[a][b] = w; //有向图

}
void Visit(int v,FILE *fo){
    fprintf(fo, "%d ", v);
}

void dfs(Graph G,int V,FILE *fo ){
    
    Visit(V,fo);
    vis[V] = true;
    for (int i = 0; i < G->v;i++){
        if(G->g[V][i]<inf&& (!vis[i])){     
            dfs(G,i,fo);
        }
    }
}

int main(){

    FILE *fp,*fo;
    fp = fopen("in.txt", "r");
    fo = fopen("out.txt", "w");
    
    fscanf(fp, "%d", &n);
    fscanf(fp, "%d", &m);

    Graph G = CreateGraph();
    
    for (int i = 0; i < m;i++){
        int a, b, w;
        fscanf(fp, "%d %d %d", &a, &b, &w);
        InsertEdge(G,a, b, w);
    }
    int k;
    fscanf(fp, "%d", &k);
    while(k!=-1){
        init();
        fprintf(fo, "DFS From  %d : ",k);
        dfs(G,k,fo);
        fscanf(fp, "%d", &k);
        fprintf(fo, "\n");
    }

    fclose(fp);
    fclose(fo);
}